package com.affectu.search.fetch;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;

import com.affectu.news.News;
import com.affectu.search.fetch.thread.NewsThread;

/**
 * Oct 15, 2008
 * 
 * @author daniel nathan
 */
public class WitkeyNews extends NewsThread {

	public WitkeyNews() {
		super("http://www.witkey.com/news.html", "/article/");
	}

	@Override
	public News spideSubPage(String url) {
		News news = new News();
		news.setLink(url);
		String name = "";
		String content = "";
		try {
			OrFilter allFilter = new OrFilter();
			allFilter.setPredicates(new NodeFilter[] {
					new NodeClassFilter(Div.class),
					new NodeClassFilter(TableTag.class) });
			NodeList nodeList = new Parser(url).parse(allFilter);
			for (Node node : nodeList.toNodeArray()) {

				String attributeValue;
				if (node instanceof Div) {
					Div divNode = (Div) node;
					attributeValue = divNode.getAttribute("class");
					if ("mleft_tle".equals(attributeValue)) {
						// ...得到新闻标题(纯文本)
						name = divNode.toPlainTextString();
						this.log(name);
						news.setName(name);
					} else if ("margin:8px 0px 0px 0px; line-height:26px; font-size:14px; word-spacing:5px;"
							.equals(divNode.getAttribute("style"))) {
						// ...得到金额(纯文本)
						content = divNode.toHtml();
						this.log(content);
						news.setContent(content);
					}

				} else if (node instanceof TableTag) {
					TableTag tableNode = (TableTag) node;
					attributeValue = tableNode.getAttribute("class");
					if ("taskinfotable".equals(attributeValue)) {

					}
				}

			}// end for
		} catch (Exception ex) {
			this.logAffectuException(ex, "抓取子页出错 :" + url);
		}
		return news;
	}

}
